<?php
class ss_api_root extends ss_konstrukt_component{
	protected function map($name){
		ss_error::log($name,'Mapping');
		if($name==$this->settings['server']['api']['interface']){
			return 'ss_api_interface';
		}
		return 'ss_api_master';
	}
}

class ss_api_master extends ss_konstrukt_masterComponent{
	public $mapper;
	public $data=array();
	protected $content;
	
	public function dispatch(){
		ss_error::log($this->name(),'Dispatch '.__CLASS__);
		//ss_error::log($this->name(),'Name');
		//ss_error::log($this->next(),'Next');
		if(is_numeric($this->next())){
			return parent::dispatch();
		}
		return $this->execute();
	}
	
	protected function map($name){
		ss_error::log($name,'Map '.__CLASS__);
		//ss_error::info($this->next(),'query');
		if(!is_numeric($name) & (strlen($name)==2)){
			return 'ss_api_master_version';
		} else {
			return 'ss_api_detail';
		}
	}
	
	public function get(){
		ss_error::log('GET '.__CLASS__);
		$range=$this->getRange();
		//ss_error::log(Doctrine::getTable($this->name())->getTemplate('Doctrine_Template_I18n')->getOption('fields'),'Table');
		$this->data=Doctrine::getTable($this->name())->query($this->query());
		foreach($this->data as $obj){
			$this->content[]=$obj->getJSONData();
		}
		return parent::get();
	}
	
	public function post(){
		ss_error::log('POST '.__CLASS__);
		$className=$this->name();
		$this->data=new $className();
		return parent::post();
	}
	
	protected function renderHtml(){
		ss_error::log('Render API HTML '.__CLASS__);
		$table='';
		foreach($this->content as $item){
			$row='<td><a href="'.$this->url($item['id']).'">open-></a><td>';
			foreach($item as $key=>$val){
				if(is_array($val) || is_object($val))$val=print_r($val,true);
				$row.="<td>{$val}</td>";
			}
			$table.='<tr>'.$row."</tr>\n";
		}
		if(is_array($item)) $table='<h1>'.$this->name()." master</h1><table>\n<tr><th></th><th>".implode('</th><th>',array_keys($item))."</th></tr>\n".$table."\n</table>\n";
		return $table;
	}
	
	protected function renderJson(){
		ss_error::log('Render API JSON '.__CLASS__);
		return new k_JsonResponse($this->content);
	}
	
	
	protected function postJson(){
		//ss_error::log('POST JSON '.__CLASS__);
		ss_error::log($this->name(),'postJSON handler for '.__CLASS__);
		$json=$this->data->sanitize(json_decode($this->rawHttpRequestBody(),true));
		$this->data->fromArray($json,'From Array');
		$this->data->save();
		$result=new k_JsonResponse($this->data->getJSONData());
		$result->setHeader('Location',$this->url($this->data['id']));
		$result->setStatus(201);
		return new k_JsonResponse($result);
		//ss_error::log($this->data->toArray(),'Result');
	}
}

class ss_api_detail extends ss_konstrukt_detailComponent{
	protected $mapper;
	protected $data;
	protected $content;
	
	protected function map($name){
		return 'ss_api_detail_version';
	}
	
	public function get(){
		ss_error::log('GET '.__CLASS__);
		$this->data=Doctrine::getTable($this->context->name())->query(array_merge(array('id'=>$this->name()),$this->query()))->getFirst();
		$this->content=$this->data->getJSONData();
		return parent::get();
	}
	
	protected function renderHtml(){
		ss_error::log('Render API HTML '.__CLASS__);
		$table='';
		foreach($this->content as $key=>$val){
			if(is_array($val) || is_object($val))$val=print_r($val,true);
			$table.="<tr><td>{$key}</td><td>{$val}</td></tr>\n";
		}
		$table='<h1>'.$this->name()." detail</h1><table>\n".$table."\n</table>\n";
		return $table;
	}
	
	protected function renderJson(){
		ss_error::log('Render API JSON '.__CLASS__);
		return new k_JsonResponse($this->content);
	}
	
	public function put(){
		ss_error::log('PUT '.__CLASS__);
		$this->data=Doctrine::getTable($this->context->name())->query(array_merge(array('id'=>$this->name()),$this->query()))->getFirst();
		return parent::put();
	}
	
	protected function putJson(){
		ss_error::log('PUT JSON '.__CLASS__);
		$json=$this->data->sanitize($raw=json_decode($this->rawHttpRequestBody(),true));
		//ss_error::log($raw,'Raw');
		//ss_error::log($this->data->toArray(false),'Data');
		//ss_error::log($json,'Request');
		$this->data->synchronizeWithArray($json,'Sync');
		//ss_error::log($this->data->toArray(),'Result');
		$this->data->save();
		//return new k_JsonResponse($this->data->getJSONData());
		return new k_JsonResponse(true);
	}
}
?>